package smart.rpc.factory;

import smart.rpc.client.ClientServiceInvocationHandler;
import smart.rpc.exception.SmartRPCException;

import java.lang.reflect.Proxy;

/**
 * Created By gao_e on 2020/3/8 10:09
 * 静态工厂，根据接口类获取动态代理对象
 * 获取的代理对象
 */
public class SmartRPCBeanFactory {

    private static final ClientServiceInvocationHandler INVOCATION_HANDLER = new ClientServiceInvocationHandler();

    // 根据接口获取动态代理对象
    public static <T> T getBeanByFacadeCls(Class<? extends T> cls) {
        if (!cls.isInterface())
            throw new SmartRPCException(" cls must a interface");
        Class<?>[] interfaces = new Class[]{cls};
        T proxyInstance = (T) Proxy.newProxyInstance(cls.getClassLoader(),
                interfaces, INVOCATION_HANDLER);
        System.out.println("====GetBeanByFacadeCls : " + cls.getSimpleName() + " proxy bean success ====");
        return proxyInstance;
    }

}
